package com.plsjavaweb.core;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import com.plsjavaweb.dao.DAOFactory;
import com.plsjavaweb.pojo.EmployeeSeatPOJO;

public class SmartSearchCore {
	public List<EmployeeSeatPOJO> searchWildCard(String inputSearchLiteral) {
		boolean test;
		DAOFactory daoService = new DAOFactory();
		List<EmployeeSeatPOJO> employeeSeats = new ArrayList<>();

		String searchLiteral = inputSearchLiteral.trim();
		String[] searchArray = searchLiteral.split(" ");

		if (searchArray.length > 0 && isInt(searchLiteral)) {
			employeeSeats.addAll(daoService.getEmployeeSeatDAO()
					.getEmployeeSeatsByEmployeeId(
							Integer.parseInt(searchLiteral)));

			if (employeeSeats.size() < 1) {
				employeeSeats.addAll(daoService.getEmployeeSeatDAO()
						.getEmployeeSeatsByProjectId(
								Integer.parseInt(searchLiteral)));
			}
		}

		else {
			for (int i = 0; i < searchArray.length; i++)
				try {
					String searchString = searchArray[i];
					// get list using query as firstname
					List<EmployeeSeatPOJO> resultByFirstName = daoService
							.getEmployeeSeatDAO()
							.getEmployeeSeatsByLikeEmployeeFirstName(
									searchString);
					// when there is a result
					if (resultByFirstName.size() > 0) {
						// check if there is already a list
						if (employeeSeats.size() > 0) {
							// filter results
							employeeSeats = filterResult(employeeSeats,
									resultByFirstName);
						} else {
							employeeSeats.addAll(resultByFirstName);
						}
					}

					List<EmployeeSeatPOJO> resultByLastName = daoService
							.getEmployeeSeatDAO()
							.getEmployeeSeatsByLikeEmployeeLastName(
									searchString);
					// }
					if (resultByLastName.size() > 0) {
						if (employeeSeats.size() > 0) {
							// filter results
							employeeSeats = filterResult(employeeSeats,
									resultByLastName);
						} else {
							employeeSeats.addAll(resultByLastName);
						}
						/*
						List<EmployeeSeatPOJO> result = resultByLastName;
						for (EmployeeSeatPOJO employee : resultByLastName) {
							if (employeeSeats.size() > 0) {
								// check if employee already exist on list
								int empId = employee.getEmployee()
										.getEmployeeId();

								for (EmployeeSeatPOJO employeeSeatPOJO : employeeSeats) {
									if (empId == employeeSeatPOJO.getEmployee()
											.getEmployeeId()) {
										try {
											result.remove(employee);
										} catch (Exception e) {
											System.out.println(e.getMessage());
										}
									}
								}
							}
							if (result.size() > 0) {
								employeeSeats.addAll(result);
							}
							// else {
							// employeeSeats.addAll(resultByLastName);
							// }
						}

					*/}
					// get employees by project
					List<EmployeeSeatPOJO> resultByProjectName=daoService.getEmployeeSeatDAO()
							.getEmployeeSeatsByProjectName(searchString);
					if(resultByProjectName.size()>0){
						if(employeeSeats.size()>0){
							try {
								employeeSeats=filterResult(employeeSeats, resultByProjectName);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}else{
							employeeSeats.addAll(resultByProjectName);
						}
					}
					// }
				} catch (Exception e) {
					System.out.println(e.getLocalizedMessage());
				}
		}

		// HashSet<EmployeeSeatPOJO> hsEmpSeat = new HashSet<>();
		// for (EmployeeSeatPOJO employeeSeatPOJO : employeeSeats) {
		// hsEmpSeat.add(employeeSeatPOJO);
		// }
		//
		// employeeSeats.clear();
		// employeeSeats.addAll(hsEmpSeat);
		//
		return employeeSeats;
	}

	private boolean isInt(String input) {
		try {
			Integer.parseInt(input);
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}

	// assumption on calling this method employee seat and search result is not
	// null
	private List<EmployeeSeatPOJO> filterResult(
			List<EmployeeSeatPOJO> employeeSeat,
			List<EmployeeSeatPOJO> searchResult) {
		List<EmployeeSeatPOJO> result = searchResult;
		try {
			for (EmployeeSeatPOJO employeeSeatPOJO : searchResult) {
				int empID = employeeSeatPOJO.getEmployee().getEmployeeId();
				for (EmployeeSeatPOJO employee : employeeSeat) {
					if (empID == employee.getEmployee().getEmployeeId()) {
						try {
							result.remove(employeeSeatPOJO);
							// result.remove(employee);
						} catch (Exception e) {
							System.out.println(e.getMessage().toString());
						}
					}
				}

			}
			if (result.size() > 0) {
				employeeSeat.addAll(result);
			}
		} 
		catch (Exception e) {
			
		}

		return employeeSeat;
	}
}
